<template>
  <div class="app-container">
    <!-- 条件栏 -->
    <div class="filter-wrapper">
      <NewPageFilter
        ref="pageFilter"
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.searchList"
        :filter-btn="filterInfo.searchBtn"
        @selectFiscalPeriodInfo="search"
      >
        <template v-slot:filter-operateTime>
          <el-date-picker
            v-model="operateTime"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="至"
            :picker-options="$fn.pickerOptionsFn()"
            align="right"
            start-placeholder="操作时间开始日期"
            end-placeholder="操作时间结束日期"
            :shortcuts="$fn.pickerOptionsFn()"
            :default-time="[
              new Date(2000, 1, 1, 0, 0, 0),
              new Date(2000, 1, 1, 23, 59, 59),
            ]"
            @change="timeChange"
          ></el-date-picker>
        </template>
      </NewPageFilter>
    </div>

    <div class="filter-wrapper">
      <NewPageFilter :filter-btn="filterInfo.btnList">
        <template v-slot:filter-print>
          <el-button
            v-if="$permission('retrievalDetails:printDetail')"
            @click="print"
            >打印</el-button
          >
        </template>
        <template v-slot:filter-excel>
          <el-button
            v-if="$permission('retrievalDetails:excelDetail')"
            @click="exportExcel"
            >导出excel</el-button
          >
        </template>
        <template v-slot:filter-excelAccountingDetailList>
          <el-button
            v-if="$permission('retrievalDetails:excelAccountingDetailList')"
            @click="exportExcelAccountingDetailList"
            >导出辅助核算明细</el-button
          >
        </template>
      </NewPageFilter>
    </div>
    <!-- 表格 -->
    <table-container
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :summary="true"
      :summary-field-list="tableInfo.summaryFieldList"
      :tab-index="false"
      :check-box="false"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :pager="false"
    >
    </table-container>
  </div>
</template>

<script setup name="retrievalDetails">
import {
  deptDeliveryDetailsSummaryExcel,
  printDeptDeliveryDetailsSummary,
  selectDeptDeliveryDetailsSummary,
  deptDeliveryDetailsSummaryExcelAccountingDetailList,
} from '@/api/dataQuery.js'
import { fatchTemplateByCode } from '@/api/base'
import { getDepotAll, getDeptAll } from '@/api/core'
const { proxy } = getCurrentInstance()

const filterInfo = ref({
  query: {
    sort: 'create_time desc',
    startDate: '',
    endDate: '',
    depotId: proxy.$store.state.user.userInfo.coreResourcesVO
      ? proxy.$store.state.user.userInfo.coreResourcesVO.useTypeId.indexOf(
          '1'
        ) !== -1
        ? proxy.$store.state.user.userInfo.coreResourcesVO.resourceId
        : ''
      : '',
  },
  copyQuery: {},
  searchList: [
    {
      label: '统计时间',
      slot: true,
      value: 'operateTime',
      event: search,
    },
    {
      label: '会计期间',
      value: 'fiscalPeriodName',
      asValue: 'fiscalPeriodName',
      componentType: 'select-fiscal-period',
      query: {
        fiscalType: 1,
      }, //1 = 物资 2 = 资产
      checkBox: false,
      highlightRow: true,
    },
    {
      label: '出库仓库',
      value: 'depotName',
      asValue: 'depotId',
      componentType: 'slip-select',
      query: {
        depotAuth: 'id',
        status: 1,
        useTypeId: '1',
      },
      api: getDepotAll,
      event: search,
    },
    {
      label: '使用部门',
      value: 'deptIds',
      componentType: 'slip-select',
      query: {
        deptAuth: 'id',
        status: 1,
      },
      api: getDeptAll,
      multiple: true,
      event: search,
    },
  ],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      value: 'print',
      type: 'slot',
    },
    {
      value: 'excel',
      type: 'slot',
    },
    {
      value: 'excelAccountingDetailList',
      type: 'slot',
    },
  ],
})
const operateTime = ref([])

const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: '',
  summaryFieldList: [
    { value: 'issueAmount', formatter: proxy.$fn.priceFormatter },
    { value: 'returnAmount', formatter: proxy.$fn.priceFormatter },
    { value: 'sumAmount', formatter: proxy.$fn.priceFormatter },
  ],
  fieldList: [
    { label: '编码', value: 'deptCode' },
    { label: '部门', value: 'deptName' },
    { label: '出库金额', value: 'issueAmount' },
    { label: '退库金额', value: 'returnAmount' },
    { label: '汇总', value: 'sumAmount' },
  ],
})

onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})

function getList() {
  tableInfo.value.api = selectDeptDeliveryDetailsSummary
  tableInfo.value.refresh = Math.random()
}

function search() {
  console.log(123)
  if (
    !filterInfo.value.query.fiscalPeriodName &&
    !filterInfo.value.query.startDate
  ) {
    proxy.$message({
      message: '请选择会计区间或操作时间',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  } else if (
    filterInfo.value.query.fiscalPeriodName &&
    filterInfo.value.query.startDate
  ) {
    proxy.$message({
      message: '会计区间或操作时间只能同时选一个',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  if (!filterInfo.value.query.depotId) {
    proxy.$message({
      message: '请选择出库仓库',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  tableInfo.value.initCurpage = Math.random()
  getList()
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  operateTime.value = []
}
function exportExcel() {
  if (
    !filterInfo.value.query.fiscalPeriodName &&
    !filterInfo.value.query.startDate
  ) {
    proxy.$message({
      message: '请选择会计区间或操作时间',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  } else if (
    filterInfo.value.query.fiscalPeriodName &&
    filterInfo.value.query.startDate
  ) {
    proxy.$message({
      message: '会计区间或操作时间只能同时选一个',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  deptDeliveryDetailsSummaryExcel(
    proxy.$fn.delEmptyAttribute(filterInfo.value.query)
  ).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function exportExcelAccountingDetailList() {
  if (
    !filterInfo.value.query.fiscalPeriodName &&
    !filterInfo.value.query.startDate
  ) {
    proxy.$message({
      message: '请选择会计区间或操作时间',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  } else if (
    filterInfo.value.query.fiscalPeriodName &&
    filterInfo.value.query.startDate
  ) {
    proxy.$message({
      message: '会计区间或操作时间只能同时选一个',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  deptDeliveryDetailsSummaryExcelAccountingDetailList(
    proxy.$fn.delEmptyAttribute(filterInfo.value.query)
  ).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}

function timeChange(val) {
  if (val && val.length) {
    filterInfo.value.query.startDate = val[0]
    filterInfo.value.query.endDate = val[1]
  } else {
    filterInfo.value.query.startDate = ''
    filterInfo.value.query.endDate = ''
  }
  search()
}
//打印
function print() {
  if (
    !filterInfo.value.query.fiscalPeriodName &&
    !filterInfo.value.query.startDate
  ) {
    proxy.$message({
      message: '请选择会计区间或操作时间',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  } else if (
    filterInfo.value.query.fiscalPeriodName &&
    filterInfo.value.query.startDate
  ) {
    proxy.$message({
      message: '会计区间或操作时间只能同时选一个',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  fatchTemplateByCode({
    resourceCode: 'retrievalDetails:printDetail',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp)
    }
  })
}
function getPrintData(temp) {
  printDeptDeliveryDetailsSummary(filterInfo.value.query).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
</script>

<style scoped></style>
